package dao

import (
	"gitee.com/cyanbleeze/go-image-management/internal/model"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
)

func (d *Dao) CreatePicture(title string, imageUrl string, desc string, uID uint, cID uint) error {
	return model.Picture{
		Title:      title,
		ImageUrl:   imageUrl,
		Desc:       desc,
		UserID:     uID,
		CategoryID: cID,
	}.Create(d.engine)
}

func (d *Dao) UpdatePicture(id uint, title string, imageUrl string, desc string, cID uint) error {
	picture := model.Picture{
		Title:      title,
		ImageUrl:   imageUrl,
		Desc:       desc,
		CategoryID: cID,
	}
	picture.ID = id
	return picture.Update(d.engine)
}

func (d *Dao) UpdatePicturePublic(id uint, public bool) error {
	picture := model.Picture{
		Public: public,
	}
	picture.ID = id
	return picture.UpdatePublic(d.engine)
}

func (d *Dao) DeletePicture(id uint, uID uint) error {
	picture := model.Picture{
		UserID: uID,
	}
	picture.ID = id
	return picture.Delete(d.engine)
}

func (d *Dao) GetPicture(id uint) (model.Picture, error) {
	picture := model.Picture{}
	picture.ID = id
	return picture.Get(d.engine)
}

func (d *Dao) GetPictureByUID(id uint, uID uint) (model.Picture, error) {
	picture := model.Picture{
		UserID: uID,
	}
	picture.ID = id
	return picture.Get(d.engine)
}
func (d *Dao) GetPublicPicture(id uint) (model.Picture, error) {
	picture := model.Picture{
		Public: true,
	}
	picture.ID = id
	return picture.Get(d.engine)
}
func (d *Dao) GetPictureList(cID uint, uID uint, page int, pageSize int) ([]*model.Picture, error) {
	picture := model.Picture{
		CategoryID: cID,
		UserID:     uID,
	}
	return picture.List(d.engine, app.GetPageOffset(page, pageSize), pageSize)
}
func (d *Dao) GetPublicPictureList(page int, pageSize int) ([]*model.Picture, error) {
	picture := model.Picture{
		Public: true,
	}
	return picture.List(d.engine, app.GetPageOffset(page, pageSize), pageSize)
}
func (d *Dao) GetPictureListInID(ids [][]interface{}, page int, pageSize int) ([]*model.Picture, error) {
	picture := model.Picture{}
	return picture.ListInID(d.engine, ids, app.GetPageOffset(page, pageSize), pageSize)
}

func (d *Dao) CountAllPictureList(uID uint) (int, error) {
	picture := model.Picture{
		UserID: uID,
	}
	return picture.Count(d.engine)
}

func (d *Dao) CountPictureListByCategoryID(uID uint, cID uint) (int, error) {
	picture := model.Picture{
		UserID:     uID,
		CategoryID: cID,
	}
	return picture.Count(d.engine)
}
func (d *Dao) CountAllPublicList() (int, error) {
	picture := model.Picture{
		Public: true,
	}
	return picture.Count(d.engine)
}
